﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;

namespace ArchiveSystem.ShowBusy
{

    public partial class Busy
    {
        
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        extern static IntPtr GetForegroundWindow();

        public IntPtr _hWnd;

        BusyForm bf=null;
        BackgroundWorker worker;
        MyThread m;
        int progressBarMaximum = 0;
        int progressBarValue = 0;
        public string operate;
        public string Communion;

        /// <summary>
        /// hWnd 为当前windows窗口句柄，为了防止程序执行后，窗口发生切换
        /// </summary>
        /// <param name="hWnd"></param>
        public Busy(IntPtr hWnd)
        {
            //bf = new BusyForm(this);
            _hWnd = hWnd;
        }

        private void DoWork(object sender, DoWorkEventArgs e)
        {
            bf = new BusyForm(this);
            bf.SetProgressBarMaximum(progressBarMaximum);
            bf.SetProgressBarValue(progressBarValue);
            bf.myThread = m;
            bf.ShowDialog();
        }

        public  void Run()
        {

            worker = new BackgroundWorker();
            worker.WorkerReportsProgress = true;
            worker.WorkerSupportsCancellation = true;
            worker.DoWork += new DoWorkEventHandler(DoWork);
            worker.RunWorkerAsync();

            Thread.Sleep(50);

        }
        
        public  void Run(int maximun)
        {
            progressBarMaximum = maximun;
            Run();
            
        }

        public void Run(MyThread myThread)
        {
            m = myThread;
            m.busy = this;
            Run();

        }

        public void Run(MyThread myThread, int maximun)
        {
            m = myThread;
            progressBarMaximum = maximun;
            Run();

        }

        public void Run(int maximun, int _value)
        {
            progressBarMaximum = maximun;
            progressBarValue = _value;
            Run();
        }

        public void SetProgressBarMaximum(int maximun)
        {
            progressBarMaximum = maximun;
            bf.SetProgressBarMaximum(progressBarMaximum);
        }

        public  void AddProgressBarValue(int StepValue)
        {
            try
            {
                bf.AddProgressBarValue(StepValue);
            }
            catch
            { }
        }

        public void CallCommunion(string communion)
        {
            try
            {
                bf.CallCommunion(communion);
            }
            catch
            { }
        }

        //delegate void Callback(double opacity);

        //public void SetOpacity(double opacity)
        //{
        //    if (bf.InvokeRequired)
        //    {
        //        Callback cb = new Callback(SetOpacity);
        //        bf.Invoke(cb, new object[] { opacity });
        //        return;
        //    }
        //    bf.Opacity = opacity;
        //}

        public void Hide()
        {
            //if (bf != null)
            //    SetOpacity(0.0);
            operate = "hide";
        }

        public  void Stop()
        {
            
            ShowRunResult(null);

        }

        delegate void CloseFormCallback(string message);

        public  void CloseForm(string message)
        {
            try
            {
                if (bf == null) return;
                if (bf.InvokeRequired)
                {
                    CloseFormCallback cfcb = new CloseFormCallback(CloseForm);
                    bf.Invoke(cfcb, new object[] { message });
                }
                else
                {
                    if (message != null)
                        MessageBox.Show(bf, message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                   
                    bf.CloseForm();
                    bf.Dispose();
                    bf = null;
                }
                if (message != null)
                {
                    //IntPtr hWnd = GetForegroundWindow();
                    //if (!hWnd.Equals(_hWnd))
                        SetForegroundWindow(_hWnd);
                    //SendKeys.Send("^u");
                }
            }
            catch
            { 
            }
        }

        public  void ShowRunResult(string message)
        {
            try
            {
                CloseForm(message);

                if (m != null)
                {
                    try
                    {
                        m.Stop();
                        m = null;
                    }
                    catch { }
                }

                if (worker == null || bf == null) return;

                progressBarMaximum = 0;
                worker.DoWork -= new DoWorkEventHandler(DoWork);
                worker.CancelAsync();
                worker.Dispose();
                worker = null;
               
            }
            catch
            {
                progressBarMaximum = 0;
                if (worker == null) return;
                worker.Dispose();
                worker = null;
                CloseForm(null);
                // MessageBox.Show(ex.ToString());
            }
            
        }

    }
}
